﻿using CommunityToolkit.Mvvm.ComponentModel;
using Prism.Common;
using Prism.Mvvm;
using Prism.Navigation;
using Prism.Navigation.Regions;
using System;
using System.Reflection.PortableExecutable;

namespace Ku.AppCore
{
    /// <summary>
    /// ,IConfirmNavigationRequest
    /// </summary>
    public partial class ViewModelBase : ObservableObject, INavigationAware
    {
        #region  添加新的属性来保存视图的信息
        //[ObservableProperty]
        //private string _TabTitle = "【默认名称】";

        private string headerName;
        /// <summary>
        /// 选项卡名称
        /// </summary>
        public string HeaderName
        {
            get => headerName;
            set
            {
                SetProperty(ref headerName, value);
            }
        }

        private string viewName;
        /// <summary>
        /// 视图名称
        /// </summary>
        public string ViewName
        {
            get => viewName;
            set
            {
                SetProperty(ref viewName, value);
            }
        }


        /// <summary>
        /// 图标
        /// </summary>
        public object IconBrush { get; set; }

        #endregion

        protected ViewModelBase()
        {


        }

        #region  INavigationAware 接口实现

        /// <summary>
        /// 触发时间：导航完成前
        /// 作用：接收用户传递参数以及是否允许导航等控制
        /// </summary>
        /// <param name="navigationContext"></param>
        public virtual void OnNavigatedTo(NavigationContext navigationContext)
        {
            var _headerName = navigationContext.Parameters["HeaderName"] as string;
            if (!string.IsNullOrEmpty(_headerName))
                HeaderName = _headerName;

            var _viewName = navigationContext.Parameters["ViewName"] as string;
            if (!string.IsNullOrEmpty(_viewName))
                ViewName = _viewName;

            var _iconBrush = navigationContext.Parameters["IconBrush"] as string;
            if (!string.IsNullOrEmpty(_iconBrush))
                IconBrush = _iconBrush;

        }

        /// <summary>
        /// 触发时间：导航页面之前触发
        /// 作用：是否重用视图。
        ///     返回 true 导航服务会重用这个视图实例进行导航，返回 false 导航服务会创建一个新的视图实例。
        /// </summary>
        /// <param name="navigationContext"></param>
        /// <returns></returns>
        public virtual bool IsNavigationTarget(NavigationContext navigationContext)
        {
            ViewName = navigationContext.NavigatedName();
            //重用 实例
            return true;
        }

        /// <summary>
        ///触发时间：离开时被调用
        ///作用：取消事件订阅或释放资源、清理操作
        /// </summary>
        /// <param name="navigationContext"></param>
        public virtual void OnNavigatedFrom(NavigationContext navigationContext)
        {

        }

        #endregion

        //public virtual void ConfirmNavigationRequest(NavigationContext navigationContext, Action<bool> continuationCallback);
    }


}
